﻿/*
 *  Author: Louis A. Leskoviansky
 *  Company: Artemis Solutions Group (http://www.artemis-solutions.com)
 */


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;

namespace AjaxControlFramework
{
    [ParseChildren(ChildrenAsProperties = true, DefaultProperty = "Scripts")]
    public class ScriptManager : Control
    {
        //------// Properties \\--------------------------------------------\\
        private List<BaseScriptControl> _scripts = new List<BaseScriptControl>(); // default value

        [PersistenceMode(PersistenceMode.InnerProperty)]
        public virtual List<BaseScriptControl> Scripts
        {
            get { return _scripts; }
            set 
            {
                if (value == null)
                {
                    _scripts = new List<BaseScriptControl>();
                }
                else
                {
                    _scripts = value;
                }
            }
        }


        private ScriptRenderLocation _renderLocation = ScriptRenderLocation.Body;
        public virtual ScriptRenderLocation RenderLocation
        {
            get { return _renderLocation; }
            set { _renderLocation = value; }
        }
        //------\\ Properties //--------------------------------------------//



        //------// Constructors \\------------------------------------------\\
        public ScriptManager()
        { }
        //------\\ Constructors //------------------------------------------//



        //------// Methods \\-----------------------------------------------\\
        protected override void OnLoad(EventArgs e)
        {
            foreach (BaseScriptControl script in Scripts)
            {
                script.ValidateProperties();

                if (!InternalScriptManager.Instance.RegisteredScripts.ContainsKey(script.Name))
                {
                    ScriptReference scriptReference = script.GenerateScriptReference();

                    if (scriptReference != null)
                    {
                        InternalScriptManager.Instance.RegisterScript(scriptReference, RenderLocation);
                    }
                }
                else if (InternalScriptManager.Instance.RegisteredScripts.ContainsKey(script.Name) && InternalScriptManager.Instance.RegisteredScripts[script.Name].IsRendered)
                {
                    continue;
                }
            }

            base.OnLoad(e);
        }


        public override void RenderControl(HtmlTextWriter writer)
        {
            List<ScriptReference> references = new List<ScriptReference>();

            foreach (BaseScriptControl script in Scripts)
            {
                if (InternalScriptManager.Instance.RegisteredScripts.ContainsKey(script.Name))
                {
                    references.Add(InternalScriptManager.Instance.RegisteredScripts[script.Name]);
                }
            }


            references.Sort(new ScriptReferenceDependencyComparer());


            foreach (ScriptReference reference in references)
            {
                BaseScriptControl currentScript = Scripts.Find(new Predicate<BaseScriptControl>(delegate(BaseScriptControl s) { return s.Name == reference.Name; }));

                if (currentScript != null && RenderLocation == ScriptRenderLocation.Here)
                {
                    try
                    {
                        currentScript.RenderControl(writer);
                        writer.WriteLine();
                    }
                    catch (System.Exception) { /* Don't try rendering the script. */ }
                }
            }
        }
        //------\\ Methods //-----------------------------------------------//
    }
}
